{extend name="public:base" /}
{block name="content"}	

<div class="page-header">
	<h1>	
		{$breadcrumb1}
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			{$breadcrumb2}
		</small>
	</h1>
</div>

<div class="page-header">	
	<a href="{:url('Goods/add')}" class="btn btn-primary">新增</a>
	<span href="{:url('Goods/copy_goods')}" class="btn btn-primary copy">复制</span>
</div>	
	
<table class="table table-striped table-bordered table-hover search-form">
	<thead>
		<input name="type" type="hidden"  value="search" />
		<th><input name="name" type="text" placeholder="输入商品名称" value="{:input('name')}" /></th>
		<th>    				
			<select name="category">
				<option value="">全部分类</option>			
				<?php $input_cid=input('category'); ?>				
				{volist name="category" id="cat"}
					<option {if condition="$input_cid eq $cat['id']"} selected="selected"{/if} value="{$cat.id}">{$cat.title_show}</option>
				{/volist}
				
			</select>
		</th>	
		<th>    				
			<select name="status">
				<option value="">全部状态</option>							
				<option {if condition="input('status') eq 1"} selected="selected"{/if}value="1">启用</option>
				<option {if condition="input('status') eq 2"} selected="selected"{/if} value="2">停用</option>
			</select>
		</th>
		<th>
			<a class="btn btn-primary" href="javascript:;" id="search" url="{:url('Goods/index')}">查询</a>
		</th>
	</thead>
</table>	
	

	
<div class="row">
	<div class="col-xs-12">	
		<div>
			<table id="table" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th class="center">
							<label>
								<input type="checkbox" class="ace check-all" />
								<span class="lbl"></span>
							</label>
						</th>											
						<th>ID</th> 
						<th>图片</th> 
						<th>商品名称</th> 			
						<th>价格</th>
						<th>数量</th>
						<th>排序</th>
						<th>状态</th>						
						<th>操作</th>				
					</tr>
				</thead>
				<tbody>
						{volist name="list" id="v" empty="$empty"}
						<tr>		
							<td class="center">
							<label>
								<input class="ace ids" type="checkbox" name="id[]" value="{$v.goods_id}"/>
								<span class="lbl"></span>
							</label>
							</td>				
							<td>{$v.goods_id}</td>
							<td>
								{if condition="$v['image']"}
		                  			<img src="IMG_ROOT{$v.image|resize=50,50}" />
			                  	{else /}
			                  		<img src="__PUBLIC__/image/no_image_50x50.jpg" />
			                  	{/if}
							</td>
							<td>{$v.name} 
								{if condition="$v['shipping'] eq 0"}
								<span class="warning">免配送</span> 								
								{/if}
								{if condition="$v['subtract'] eq 0"}
								<span class="warning">不减库存</span> 								
								{/if}
							</td>
							
							<td>
								<input name="price" type="text" size="2" class="price" goods_id='{$v.goods_id}' value="{$v.price}" />
							</td>
							<td>							
								<input name="quantity" type="text" size="2" class="quantity" goods_id='{$v.goods_id}' value="{$v.quantity}" />
							</td>
							<td>
								<input name="sort" type="text" size="1" class="sort" goods_id='{$v.goods_id}' value="{$v.sort_order}" />								
							</td>
							<td>
								{switch name="v.status"}
									{case value="1"}<a href='{:url("Goods/set_status",array("id"=>$v["goods_id"],"status"=>2))}'><span class="btn btn-xs btn-info"><i class="fa fa-unlock bigger-120"></i></span></a>{/case}
									{case value="2"}<a href='{:url("Goods/set_status",array("id"=>$v["goods_id"],"status"=>1))}'><span class="btn btn-xs btn-danger"><i class="fa fa-lock bigger-120"></i></span></a>{/case}
								{/switch}
							</td>
							<td>
								
								
								<div class="btn-group">
									<button class="btn btn-xs btn-info">
										<i class="fa fa-edit bigger-120"></i>
									</button>
									<button class="btn btn-info btn-xs dropdown-toggle no-radius" data-toggle="dropdown" aria-expanded="false">
										<span class="bigger-110 ace-icon fa fa-caret-down icon-only"></span>
									</button>
									<ul class="dropdown-menu dropdown-info dropdown-menu-right">																
										<li>
											<a href='{:url("Goods/edit_general",array("id"=>$v["goods_id"]))}'>基本信息</a>
										</li>									
																		
										<li>
											<a href='{:url("Goods/edit_links",array("id"=>$v["goods_id"]))}'>关联</a>
										</li>
										<li>
											<a href='{:url("Goods/edit_option",array("id"=>$v["goods_id"]))}'>选项</a>
										</li>
										<li>
											<a href='{:url("Goods/edit_discount",array("id"=>$v["goods_id"]))}'>折扣</a>
										</li>
										<li>
											<a href='{:url("Goods/edit_image",array("id"=>$v["goods_id"]))}'>商品相册</a>
										</li>
										<li>
											<a href='{:url("Goods/edit_mobile",array("id"=>$v["goods_id"]))}'>手机端描述</a>
										</li>									
									</ul>
								</div>
								
								<a class="delete btn btn-xs btn-danger" href='{:url("Goods/del",array("id"=>$v["goods_id"]))}' >
									<i class="fa fa-trash bigger-120"></i>
								</a>
								
							</td>
						</tr>
						{/volist}
						
						<tr>
							<td colspan="20" class="page">{$list->render()}</td>
						</tr>
						<tr>
							<td colspan="20">总计 {$list->total()|default="0"} 个商品</td>
						</tr>
				</tbody>
				
			</table>
		</div>
	</div>
</div>
{/block}
{block name="javascript"}
<style>
.warning{
	border:1px solid red;
	color:#f60;
}	
</style>
<script>
$(function(){

	$('.price').blur(function(){
		
		$.post(
			"{:url('Goods/update_price')}",
			{price:$(this).val(),goods_id:$(this).attr('goods_id')},
			function(data){
				if(data){
					window.location.reload();
				}
			}
		);
	});
	
	$('.quantity').blur(function(){
		
		$.post(
			"{:url('Goods/update_quantity')}",
			{quantity:$(this).val(),goods_id:$(this).attr('goods_id')},
			function(data){
				if(data){
					window.location.reload();
				}
			}
		);
	});
	
	$('.sort').blur(function(){
		
		$.post(
			"{:url('Goods/update_sort')}",
			{sort:$(this).val(),goods_id:$(this).attr('goods_id')},
			function(data){
				if(data){
					window.location.reload();
				}
			}
		);
	});
	
	
	$("#search").click(function () {
        var url = $(this).attr('url');
        var query = $('.search-form').find('input,select').serialize();
        query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, '');
        query = query.replace(/^&/g, '');
        if (url.indexOf('?') > 0) {
            url += '&' + query;
        } else {
            url += '?' + query;
        }
        window.location.href = url;
    });
	
	$('.copy').click(function(){
		
		if($('input:checked').size()==0){
			alert('请选择一个商品');
			return ;
		}
		
		$.ajax({
		url: "{:url('Goods/copy_goods')}",
		type: 'post',
		data: $('input:checked'),
		dataType: 'json',
		beforeSend: function() {
			$('.copy').attr('disabled', true);
			$('.copy').after('<span class="wait">&nbsp;<img src="__PUBLIC__/image/loading.gif" alt="" /></span>');
		},	
		complete: function() {
			$('.copy').attr('disabled', false); 
			$('.wait').remove();
		},			
		success: function(json) {
			$('.warning, .error').remove();
					
			if (json['redirect']) {				
				
				location = json['redirect']
								
			} 
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert('修改失败');
		}
	});	
	});
});		
</script>
{/block}